//
//  ViewController.swift
//  WriteFileToSandbox
//
//  Created by liy on 2025/1/18.
//

import UIKit

class ViewController: UIViewController {
  override func viewDidLoad() {
    super.viewDidLoad()

    let sp = NSSearchPathForDirectoriesInDomains(.documentDirectory, .allDomainsMask, true)
    if sp.count > 0 {
      let url = URL(fileURLWithPath: "\(sp[0])/data.txt")
      print(url)

      let data = NSMutableData()
      data.append("hello swift !".data(using: .utf8, allowLossyConversion: true)!)
      data.write(toFile: url.path, atomically: true)

      print("end ")

      // 读取文件
			
      if let data2 = NSData(contentsOfFile: url.path),
         let str = String(data: data2 as Data, encoding: .utf8) {
        print(str)
      }
    }
  }

  @IBAction func readFile(_ sender: Any) {
  }
}
